import Vue from 'vue'
import VueRouter from 'vue-router'
import { getCookie } from '@/utils/cookie'
import { Message } from 'element-ui'
import { Toast } from 'vant'

// 主页
import Home from '../views/Home.vue'
import HomeBar from '../components/HomeBar.vue' // 主页头部
import HomeBottom from '../components/HomeBottom.vue' // 主页底部
// 商城
import Mail from '../views/mail/Mail.vue'
import ProductItem from '../views/mail/components/category/ProductItem.vue'
import ProductDetail from '../views/mail/components/category/ProductDetail.vue'
import AlbumItem from '../views/mail/components/album/AlbumItem.vue'
import AlbumDetail from '../views/mail/components/album/AlbumDetail.vue'
import ConfirmProducts from '../views/mail/components/category/ConfirmProducts.vue'
import ShowCollection from '../views/mail/components/category/ShowCollection.vue'
import HadPassOn from '../views/mail/components/category/HadPassOn.vue'
import PayCompleted from '../views/mail/components/category/PayCompleted.vue'
import PayCompletedBefore from '../views/mail/components/category/PayCompletedBefore.vue'
import TempCheck from '../views/mail/components/category/TempCheck.vue'
import PassOn from '../views/mail/components/category/PassOn.vue'
import WxPay from '../views/mail/components/category/WxPay.vue'
import InviteCode from '../views/mail/components/invite/InviteCode.vue'
import InviteFriend from '../views/mail/components/invite/InviteFriend.vue'
import InviteAssist from '../views/mail/components/invite/InviteAssist.vue'
import InviteRecord from '../views/mail/components/invite/InviteRecord.vue'
import InviteShare from '../views/mail/components/invite/InviteShare.vue'
import BindCard from '../views/mail/components/bindCard/BindCard.vue'
import SelectCard from '../views/mail/components/bindCard/SelectCard.vue'
import ConfirmCardPay from '../views/mail/components/bindCard/ConfirmCardPay.vue'
const InviteRuleDesc = () => import('../views/mail/components/invite/InviteRuleDesc.vue')

// 关于我们
import AboutUs from '../views/about/AboutUs.vue'
import HelpCenter from '../views/about/HelpCenter.vue'
import Privacy from '../views/about/Privacy.vue'
import UserAgreement from '../views/about/UserAgreement.vue'
// 个人中心
import PersonalCenter from '../views/personalCenter/PersonalCenter.vue'
import PersonalCollection from '../views/personalCenter/PersonalCollection.vue'
import PersonalCurrentlyOwned from '../views/personalCenter/PersonalCurrentlyOwned.vue'
import PersonalFavorite from '../views/personalCenter/PersonalFavorite.vue'
import PersonalHomePage from '../views/personalCenter/PersonalHomePage.vue'
import PersonalModify from '../views/personalCenter/PersonalModify.vue'
import PersonalNicknameModify from '../views/personalCenter/mobileDetailsModify/PersonalNicknameModify.vue'
import PersonalDescriptionModify from '../views/personalCenter/mobileDetailsModify/PersonalDescriptionModify.vue'
import Cancelled from '../views/personalCenter/order/Cancelled.vue'
import Unpaid from '../views/personalCenter/order/Unpaid.vue'
import PersonalCollectionPage from '../views/personalCenter/PersonalCollectionPage.vue'
import Discount from '../views/personalCenter/discount/Discount.vue'
import TransferHistory from '../views/personalCenter/order/TransferHistory.vue'
import SyntheticHistory from '../views/personalCenter/synthetic/SyntheticHistory.vue'
import SyntheticList from '../views/personalCenter/synthetic/SyntheticList.vue'
import SyntheticAction from '../views/personalCenter/synthetic/SyntheticAction.vue'
import Address from '../views/personalCenter/address/Address.vue'
import AddressPhone from '../views/personalCenter/address/AddressPhone.vue'
// 登录、身份认证、忘记密码、区块链地址、账户余额
import Login from '../views/mobileAuthentication/Login.vue'
import Authentication from '../views/mobileAuthentication/Authentication.vue'
import Auth from '../views/pcAuthentication/Auth.vue'
import AuthOK from '../views/pcAuthentication/AuthOK.vue'
import Blockchain from '../views/mobileAuthentication/Blockchain.vue'
import SetPassword from '../views/pwdSet/SetPassword.vue'
import ResetPassword from '../views/pwdSet/ResetPassword.vue'
import Account from '../views/account/Account.vue'
// 搜索页
import Search from '../views/search/Search.vue'
// 验证码界面
import GetCode from '../views/mobileAuthentication/GetCode.vue'

// 公告列表页和公告详情页
import NoticeList from '../views/notice/NoticeList.vue'
import NoticeItem from '../views/notice/NoticeItem.vue'

// 交易
import Trade from '../views/iframe/Trade.vue'

//404
import NoFound from '../views/404/404.vue'
import Maintenance from '../views/404/Maintenance.vue'
Vue.use(VueRouter)
import { openprizeApi } from '@/api'
import { iscode } from '@/utils'

//canvas
import Text from '../views/canvas/Text.vue'

//app下载
import AppDownload from '../views/app/AppDownload.vue'

//获取页面权限
const openprize = async () => {
  let res = await openprizeApi()
  if (iscode(res)) {
    // router.push("/maintenance");
    if (res.result.value == 2) {
      router.push('/maintenance')
    }
  }
}
const routes = [
  // 主页
  {
    path: '/',
    name: 'Home',
    components: { default: Home, header: HomeBar, footer: HomeBottom },
    // props: {
    //   header: { colorOnScroll: 400, navbarTransparent: true },
    // },
    meta: {
      title: '主页',
      isToken: false
    }
  },

  // 商城全部
  {
    path: '/mail',
    // name: "mail",
    components: { default: Mail, header: HomeBar, footer: HomeBottom },
    meta: {
      isToken: false
    },
    children: [
      {
        path: '',
        redirect: 'productItem'
      },
      {
        path: 'productItem',
        name: 'ProductItem',
        components: { default: ProductItem },
        meta: {
          title: '商城'
        }
      },
      {
        path: 'productDetail',
        name: 'ProductDetail',
        components: { default: ProductDetail },
        meta: {
          title: '商品详情'
        }
      },
      {
        path: 'albumItem',
        name: 'AlbumItem',
        components: { default: AlbumItem },
        meta: {
          title: '商城'
        }
      },
      {
        path: 'albumDetail',
        name: 'AlbumDetail',
        components: { default: AlbumDetail },
        meta: {
          title: '专辑详情'
        }
      },
      {
        path: 'confirmProducts',
        name: 'ConfirmProducts',
        components: { default: ConfirmProducts },
        meta: {
          title: '支付界面',
          isToken: true
        }
      },
      {
        path: 'tempCheck',
        name: 'TempCheck',
        components: { default: TempCheck },
        meta: {
          title: '确认支付',
          isToken: true
        }
      },
      {
        path: 'showCollection',
        name: 'ShowCollection',
        components: { default: ShowCollection },
        meta: {
          title: '查看藏品',
          isToken: true
        }
      },
      {
        path: 'hadPassOn',
        name: 'HadPassOn',
        components: { default: HadPassOn },
        meta: {
          title: '已转赠',
          isToken: true
        }
      },
      {
        path: 'passOn',
        name: 'PassOn',
        components: { default: PassOn },
        meta: {
          title: '转赠藏品',
          isToken: true
        }
      },
      {
        path: 'wxPay',
        name: 'WxPay',
        components: { default: WxPay },
        meta: {
          title: '微信支付',
          isToken: true
        }
      },
      {
        path: 'inviteCode',
        name: 'InviteCode',
        components: { default: InviteCode },
        meta: {
          title: '邀请好友',
          isToken: true
        }
      },
      {
        path: 'inviteFriend',
        name: 'InviteFriend',
        components: { default: InviteFriend },
        meta: {
          title: '邀请好友助力',
          isToken: true
        }
      },
      {
        path: 'inviteAssist',
        name: 'InviteAssist',
        components: { default: InviteAssist },
        meta: {
          title: '助力',
          isToken: true
        }
      },
      {
        path: 'inviteRecord',
        name: 'InviteRecord',
        components: { default: InviteRecord },
        meta: {
          title: '邀请记录',
          isToken: true
        }
      },
      {
        path: 'inviteShare',
        name: 'InviteShare',
        components: { default: InviteShare },
        meta: {
          title: '分享页',
          isToken: true
        }
      },
      {
        path: 'bindCard',
        name: 'BindCard',
        components: { default: BindCard },
        meta: {
          title: '快捷支付',
          isToken: true
        }
      },
      {
        path: 'selectCard',
        name: 'SelectCard',
        components: { default: SelectCard },
        meta: {
          title: '快捷支付',
          isToken: true
        }
      },
      {
        path: 'confirmCardPay',
        name: 'ConfirmCardPay',
        components: { default: ConfirmCardPay },
        meta: {
          title: '快捷支付',
          isToken: true
        }
      }
    ]
  },

  // 支付成功
  {
    path: '/payCompleted',
    name: 'PayCompleted',
    components: { default: PayCompleted, header: HomeBar, footer: HomeBottom },
    meta: {
      title: '支付成功',
      isToken: true
    }
  },

  // 支付成功
  {
    path: '/payCompletedBefore',
    name: 'PayCompletedBefore',
    components: { default: PayCompletedBefore },
    meta: {
      title: '支付成功',
      isToken: true
    }
  },

  // 关于我们
  {
    path: '/aboutUs',
    name: 'AboutUs',
    components: { default: AboutUs, header: HomeBar, footer: HomeBottom },
    meta: {
      title: '关于我们',
      isToken: false
    }
  },

  // 帮助中心
  {
    path: '/helpCenter',
    name: 'HelpCenter',
    components: { default: HelpCenter, header: HomeBar, footer: HomeBottom },
    meta: {
      title: '帮助中心',
      isToken: false
    }
  },

  // 隐私政策
  {
    path: '/privacy',
    name: 'Privacy',
    components: { default: Privacy, header: HomeBar, footer: HomeBottom },
    meta: {
      title: '隐私政策',
      isToken: false
    }
  },

  // 用户服务协议
  {
    path: '/userAgreement',
    name: 'UserAgreement',
    components: { default: UserAgreement, header: HomeBar, footer: HomeBottom },
    meta: {
      title: '用户协议',
      isToken: false
    }
  },

  // 个人中心
  {
    path: '/personalCenter',
    name: 'PersonalCenter',
    components: {
      default: PersonalCenter,
      header: HomeBar,
      footer: HomeBottom
    },
    meta: {
      title: '个人中心',
      isToken: true
    }
  },

  // 个人中心-我的主页
  {
    path: '/personalHomePage',
    name: 'PersonalHomePage',
    components: {
      default: PersonalHomePage,
      header: HomeBar,
      footer: HomeBottom
    },
    meta: {
      title: '我的主页',
      isToken: false
    }
  },

  // 个人中心-修改资料
  {
    path: '/personalModify',
    name: 'PersonalModify',
    components: {
      default: PersonalModify,
      header: HomeBar,
      footer: HomeBottom
    },
    meta: {
      title: '修改资料',
      isToken: true
    }
  },

  // 个人中心-我的点赞
  {
    path: '/personalFavorite',
    name: 'PersonalFavorite',
    components: {
      default: PersonalFavorite,
      header: HomeBar,
      footer: HomeBottom
    },
    meta: {
      title: '我的点赞',
      isToken: true
    }
  },

  // 个人中心-当前拥有
  {
    path: '/personalCurrentlyOwned',
    name: 'PersonalCurrentlyOwned',
    components: {
      default: PersonalCurrentlyOwned,
      header: HomeBar,
      footer: HomeBottom
    },
    meta: {
      title: '已拥有',
      isToken: true
    }
  },

  // 个人中心-优惠券
  {
    path: '/discount',
    name: 'Discount',
    components: {
      default: Discount,
      header: HomeBar,
      footer: HomeBottom
    },
    meta: {
      title: '优惠券',
      isToken: true
    }
  },

  // 个人中心-未支付
  {
    path: '/unpaid',
    name: 'Unpaid',
    components: {
      default: Unpaid,
      header: HomeBar
    },
    meta: {
      title: '未支付',
      isToken: true
    }
  },

  // 个人中心-已取消
  {
    path: '/cancelled',
    name: 'Cancelled',
    components: {
      default: Cancelled,
      header: HomeBar
    },
    meta: {
      title: '已取消',
      isToken: true
    }
  },

  // 个人中心-我的收藏
  {
    path: '/personalCollection',
    name: 'PersonalCollection',
    components: {
      default: PersonalCollection,
      header: HomeBar,
      footer: HomeBottom
    },
    meta: {
      title: '我的收藏',
      isToken: true
    }
  },

  // 个人中心-昵称修改
  {
    path: '/personalNicknameModify',
    name: 'PersonalNicknameModify',
    components: {
      default: PersonalNicknameModify,
      header: HomeBar
    },
    meta: {
      title: '修改昵称',
      isToken: true
    }
  },

  // 个人中心-个人描述修改
  {
    path: '/personalDescriptionModify',
    name: 'PersonalDescriptionModify',
    components: {
      default: PersonalDescriptionModify,
      header: HomeBar
    },
    meta: {
      title: '修改描述',
      isToken: true
    }
  },

  // 登录
  {
    path: '/login',
    name: 'Login',
    components: {
      default: Login,
      header: HomeBar
    },
    meta: {
      title: '登录',
      isToken: false
    }
  },

  // 身份认证
  {
    path: '/authentication',
    name: 'Authentication',
    components: {
      default: Authentication,
      header: HomeBar
    },
    meta: {
      title: '实名认证',
      isToken: true
    }
  },

  // 藏品详情页
  {
    path: '/personalCollectionPage',
    name: 'PersonalCollectionPage',
    components: {
      default: PersonalCollectionPage,
      header: HomeBar,
      footer: HomeBottom
    },
    meta: {
      title: '藏品详情页',
      isToken: true
    }
  },

  // 搜索界面
  {
    path: '/search',
    name: 'Search',
    components: {
      default: Search,
      header: HomeBar,
      footer: HomeBottom
    },
    meta: {
      title: '搜索页',
      isToken: false
    }
  },

  // 区块链地址
  {
    path: '/blockchain',
    name: 'Blockchain',
    components: {
      default: Blockchain,
      header: HomeBar
    },
    meta: {
      title: '区块链地址',
      isToken: true
    }
  },

  // 设置密码
  {
    path: '/setPassword',
    name: 'SetPassword',
    components: {
      default: SetPassword,
      header: HomeBar,
      footer: HomeBottom
    },
    meta: {
      title: '设置密码',
      isToken: true
    }
  },

  // 修改密码
  {
    path: '/resetPassword',
    name: 'ResetPassword',
    components: {
      default: ResetPassword,
      header: HomeBar,
      footer: HomeBottom
    },
    meta: {
      title: '修改密码',
      isToken: true
    }
  },
  // 获取验证码
  {
    path: '/getCode',
    name: 'GetCode',
    components: {
      default: GetCode,
      header: HomeBar
    },
    meta: {
      title: '获取验证码',
      isToken: true
    }
  },
  // pc 身份认证
  {
    path: '/auth',
    name: 'Auth',
    components: {
      default: Auth,
      header: HomeBar,
      footer: HomeBottom
    },
    meta: {
      title: '身份认证',
      isToken: true
    }
  },
  // pc 身份认证成功页面
  {
    path: '/auth_ok',
    name: 'AuthOK',
    components: {
      default: AuthOK,
      header: HomeBar,
      footer: HomeBottom
    },
    meta: {
      title: '身份认证',
      isToken: true
    }
  },
  // pc 账户余额界面
  {
    path: '/account',
    name: 'Account',
    components: {
      default: Account,
      header: HomeBar,
      footer: HomeBottom
    },
    meta: {
      title: '账户余额',
      isToken: true
    }
  },
  // 转赠记录
  {
    path: '/transferHistory',
    name: 'TransferHistory',
    components: {
      default: TransferHistory,
      header: HomeBar,
      footer: HomeBottom
    },
    meta: {
      title: '转赠记录',
      isToken: true
    }
  },
  // 404
  {
    path: '/noFound',
    name: 'NoFound',
    components: {
      default: NoFound
    },
    meta: {
      title: '404',
      isToken: false
    }
  },
  //维护
  {
    path: '/maintenance',
    name: 'Maintenance',
    components: {
      default: Maintenance
    },
    meta: {
      title: '系统维护',
      isToken: false
    }
  },
  //canvasText
  {
    path: '/text',
    name: 'Text',
    components: {
      default: Text
    },
    meta: {
      title: 'canvas',
      isToken: false
    }
  },
  {
    path: '/noticeList',
    name: 'NoticeList',
    components: {
      default: NoticeList,
      header: HomeBar,
      footer: HomeBottom
    },
    meta: {
      title: '公告列表',
      isToken: false
    }
  },
  {
    path: '/noticeItem',
    name: 'NoticeItem',
    components: {
      default: NoticeItem,
      header: HomeBar,
      footer: HomeBottom
    },
    meta: {
      title: '公告详情',
      isToken: false
    }
  },
  {
    path: '/syntheticHistory',
    name: 'SyntheticHistory',
    components: {
      default: SyntheticHistory,
      header: HomeBar,
      footer: HomeBottom
    },
    meta: {
      title: '合成记录',
      isToken: true
    }
  },
  {
    path: '/syntheticList',
    name: 'SyntheticList',
    components: {
      default: SyntheticList,
      header: HomeBar,
      footer: HomeBottom
    },
    meta: {
      title: '合成藏品',
      isToken: true
    }
  },
  {
    path: '/syntheticAction',
    name: 'SyntheticAction',
    components: {
      default: SyntheticAction,
      header: HomeBar,
      footer: HomeBottom
    },
    meta: {
      title: '合成',
      isToken: true
    }
  },
  {
    path: '/address',
    name: 'Address',
    components: {
      default: Address,
      header: HomeBar,
      footer: HomeBottom
    },
    meta: {
      title: '地址管理',
      isToken: true
    }
  },
  {
    path: '/addressPhone',
    name: 'AddressPhone',
    components: {
      default: AddressPhone,
      header: HomeBar,
      footer: HomeBottom
    },
    meta: {
      title: '编辑地址',
      isToken: true
    }
  },
  {
    path: '/appDownload',
    name: 'AppDownload',
    components: {
      default: AppDownload,
      header: HomeBar,
      footer: HomeBottom
    },
    meta: {
      title: 'APP',
      isToken: false
    }
  },
  {
    path: '/trade',
    name: 'Trade',
    meta: {
      title: '市场',
      isToken: false
    },
    component: Trade
  },
  {
    path: '/ruleDesc',
    name: 'RuleDesc',
    meta: {
      title: '规则说明',
      isToken: true
    },
    components: { default: InviteRuleDesc, header: HomeBar }
  }
]

const router = new VueRouter({
  routes,
  // 解决路由跳转后视口不在最顶部的问题
  scrollBehavior(to, from, savedPosition) {
    if (savedPosition) {
      return savedPosition // 按下 后退/前进 按钮时，类似浏览器的原生表现
    } else {
      return { x: 0, y: 0 } // 让页面滚动到顶部
    }
  }
})

// 解决路由重复点击报错问题
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err)
}
let screenWidth = document.body.clientWidth || document.documentElement.clientWidth

//设置路由导航拦截：
router.beforeEach((to, from, next) => {
  openprize()
  let isLogin = getCookie('token') || JSON.parse(window.localStorage.getItem('token'))
  let isToken = to.meta.isToken
  if (isToken) {
    if (!isLogin) {
      // 如果是登录页面路径，就直接跳下一步
      if (
        to.path == '/personalFavorite' ||
        to.path == '/personalCollection' ||
        to.path == '/personalCurrentlyOwned' ||
        to.path == 'unpaid' ||
        to.path == 'cancelled' ||
        to.path == '/mail/confirmProducts' ||
        to.path == '/personalCollectionPage' ||
        to.path == '/setPassword' ||
        to.path == '/resetPassword' ||
        to.path == '/personalModify' ||
        to.path == '/mail/inviteCode' ||
        to.path == '/personalCenter' ||
        to.path == '/mail/productDetail' ||
        to.path == '/discount' ||
        to.path == '/account' ||
        to.path == '/transferHistory' ||
        to.path == '/syntheticHistory' ||
        to.path == '/syntheticList' ||
        to.path == '/syntheticAction'
      ) {
        if (screenWidth <= 768) {
          Toast('您还没有登录!')
          next('/login')
        } else {
          Message({
            message: '您还没有登录!',
            iconClass: 'myErrorIcon',
            customClass: 'myError'
          })
          next('/')
        }
      } else {
        next()
      }
    } else {
      next()
    }
  } else {
    next()
  }
})

router.afterEach(() => {
  window.scrollTo(0, 0)
  // chrome
  document.body.scrollTop = 0
  // firefox
  document.documentElement.scrollTop = 0
  // safari
  window.pageYOffset = 0
})

export default router
